package com.jia.lidao.ui.order;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.jia.lidao.R;
import com.jia.lidao.UserUtils;
import com.jia.lidao.enity.DriverRespDTO;
import com.jia.lidao.enity.Drivers;
import com.jia.lidao.enity.RefreshStatusMessage;
import com.jia.lidao.network.http.BusinessManager;
import com.jia.lidao.network.http.MyCallback;
import com.jia.lidao.network.http.request.impl.DeliveryRequest;
import com.jia.lidao.network.http.request.impl.GetDriversRequest;
import com.jia.lidao.ui.BaseActivity;
import com.jia.lidao.utils.CommonUtil;
import com.jia.lidao.utils.ListUtil;
import com.jia.lidao.widget.BaseBottomDialog;
import com.jia.lidao.widget.CommonTitleBar;
import com.weigan.loopview.LoopView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by jia on 17/12/19.
 */

public class TransportActivity extends BaseActivity {


    private EditText driverNameEdt, driverPhoneEdt, driverNumEdt;

    private TextView chooseTxt;

    private CommonTitleBar commonTitleBar;

    private int drierid, orderid;

    private BaseBottomDialog baseBottomDialog;

    private TextView tvCancel,tvSure;

    private LoopView loopView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transport);
        orderid = getIntent().getIntExtra("ID", 0);
        initView();
    }

    private void initView() {
        commonTitleBar = (CommonTitleBar) findViewById(R.id.common_title_bar);
        driverNameEdt = (EditText) findViewById(R.id.car_nameEdt);
        driverPhoneEdt = (EditText) findViewById(R.id.car_phoneEdt);
        driverNumEdt = (EditText) findViewById(R.id.car_numberEdt);
        chooseTxt = (TextView) findViewById(R.id.tv_choose_driver);
        chooseTxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getDatas();
            }
        });
        commonTitleBar.setTitle("物流信息");
        commonTitleBar.setmRightTv("保存", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (drierid != 0) {
                    if (TextUtils.isEmpty(driverNameEdt.getText().toString())) {
                        showToast("司机姓名不能为空");
                        return;
                    }
                    if (TextUtils.isEmpty(driverPhoneEdt.getText().toString())) {
                        showToast("司机联系方式不能为空");
                        return;
                    }
                    if(!CommonUtil.checkPhone(driverPhoneEdt.getText().toString())){
                        showToast("手机号格式不对");
                        return;
                    }
                    if (TextUtils.isEmpty(driverNumEdt.getText().toString())) {
                        showToast("车牌号不能为空");
                        return;
                    }
                    //车牌号格式验证
                    String vehicleNoStyle = "^[\\u4e00-\\u9fa5]{1}[a-hj-zA-HJ-Z]{1}[a-hj-zA-HJ-Z_0-9]{4}[a-hj-zA-HJ-Z_0-9_\\u4e00-\\u9fa5]$";
                    Pattern pattern = Pattern.compile(vehicleNoStyle);
                    Matcher matcher = pattern.matcher(driverNumEdt.getText().toString().trim());
                    if (!matcher.matches()) {
                        showToast("车牌号格式不对！请重新填写！(字母必须大写)");
                        return;
                    }
                    showDialog("");
                    BusinessManager.getInstance().delivery(new DeliveryRequest(orderid, drierid,driverPhoneEdt.getText().toString(), driverNameEdt.getText().toString(), driverNumEdt.getText().toString()), new MyCallback<Void>() {
                        @Override
                        public void onSuccess(Void result, String message) {
                            showToast(message);
                            dismissDialog();
                            EventBus.getDefault().post(new RefreshStatusMessage());
                            finish();
                            if(ConfirmOrderActivity.instance!=null){
                                ConfirmOrderActivity.instance.finish();
                            }
                        }

                        @Override
                        public void onError(String errorCode, String message) {
                            showToast(message);
                            dismissDialog();
                        }

                        @Override
                        public void onFinished() {

                        }

                        @Override
                        public void onRefreshToken() {
                            UserUtils.getInstance().goToLogin(TransportActivity.this);
                        }
                    });
                } else {
                    if (TextUtils.isEmpty(driverNameEdt.getText().toString())) {
                        showToast("司机姓名不能为空");
                        return;
                    }
                    if (TextUtils.isEmpty(driverPhoneEdt.getText().toString())) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 showToast("司机联系方式不能为空");
                        return;
                    }
                    if(!CommonUtil.checkPhone(driverPhoneEdt.getText().toString())){
                        showToast("手机号格式不对");
                        return;
                    }
                    if (TextUtils.isEmpty(driverNumEdt.getText().toString())) {
                        showToast("车牌号不能为空");
                        return;
                    }
                    //车牌号格式验证
                    String vehicleNoStyle = "^[\\u4e00-\\u9fa5]{1}[a-hj-zA-HJ-Z]{1}[a-hj-zA-HJ-Z_0-9]{4}[a-hj-zA-HJ-Z_0-9_\\u4e00-\\u9fa5]$";
                    Pattern pattern = Pattern.compile(vehicleNoStyle);
                    Matcher matcher = pattern.matcher(driverNumEdt.getText().toString().trim());
                    if (!matcher.matches()) {
                        showToast("车牌号格式不对！请重新填写！(字母必须大写)");
                        return;
                    }
                    showDialog("");
                    BusinessManager.getInstance().delivery(new DeliveryRequest(orderid,0, driverPhoneEdt.getText().toString(), driverNameEdt.getText().toString(), driverNumEdt.getText().toString()), new MyCallback<Void>() {
                        @Override
                        public void onSuccess(Void result, String message) {
                            showToast("发货成功");
                            dismissDialog();
                            EventBus.getDefault().post(new RefreshStatusMessage());
                            finish();
                            if(ConfirmOrderActivity.instance!=null){
                                ConfirmOrderActivity.instance.finish();
                            }
                        }

                        @Override
                        public void onError(String errorCode, String message) {
                            showToast(message);
                            dismissDialog();
                        }

                        @Override
                        public void onFinished() {

                        }

                        @Override
                        public void onRefreshToken() {
                            UserUtils.getInstance().goToLogin(TransportActivity.this);
                        }
                    });
                }

            }
        });
    }

    private void getDatas(){
        showDialog("");
        BusinessManager.getInstance().getDrivers(new GetDriversRequest(), new MyCallback<DriverRespDTO>() {
            @Override
            public void onSuccess(DriverRespDTO result, String message) {
                if(result==null|| ListUtil.isEmpty(result.drivers)){
                    showToast("没有司机数据");
                    dismissDialog();
                    return;
                }
                dismissDialog();
                showBottomDialog(result.drivers);
            }

            @Override
            public void onError(String errorCode, String message) {
                showToast(message);
                dismissDialog();
            }

            @Override
            public void onFinished() {

            }

            @Override
            public void onRefreshToken() {
                UserUtils.getInstance().goToLogin(TransportActivity.this);
            }
        });
    }


    private void showBottomDialog(final List<Drivers> items){

        if(baseBottomDialog==null) {
            baseBottomDialog = new BaseBottomDialog(this);
            View view= LayoutInflater.from(this).inflate(R.layout.dialog_choose,null);
            tvCancel=(TextView)view.findViewById(R.id.tv_cancel);
            tvSure=(TextView)view.findViewById(R.id.tv_sure);
            loopView=(LoopView)view.findViewById(R.id.loopView);
            baseBottomDialog.setContentView(view);
        }
        List<String> strings=new ArrayList<>();
        for (int i=0;i<items.size();i++){
            strings.add(items.get(i).realname+"  "+items.get(i).mobile+" "+items.get(i).license);
        }
        loopView.setItems(strings);
        loopView.setNotLoop();
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                baseBottomDialog.dismiss();
            }
        });
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                drierid=items.get(loopView.getSelectedItem()).logistics_driver_id;
                driverNameEdt.setText(items.get(loopView.getSelectedItem()).realname);
                driverNumEdt.setText(items.get(loopView.getSelectedItem()).license);
                driverPhoneEdt.setText(items.get(loopView.getSelectedItem()).mobile);
                baseBottomDialog.dismiss();
            }
        });
        baseBottomDialog.show();
    }
}
